cmake_minimum_required(VERSION 3.22)

# 创建utils接口库
add_library(utils INTERFACE)

# 设置utils库的包含目录，这样其他项目可以直接引用utils中的头文件
target_include_directories(utils INTERFACE
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 自动发现并添加所有子目录作为子项目
file(GLOB SUBDIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
foreach(SUBDIR ${SUBDIRS})
    if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR} AND 
       EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt)
        add_subdirectory(${SUBDIR})
    endif()
endforeach()

# 为接口库添加所有子库的依赖
get_property(UTILS_TARGETS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY BUILDSYSTEM_TARGETS)
foreach(TARGET ${UTILS_TARGETS})
    get_target_property(TARGET_TYPE ${TARGET} TYPE)
    if(TARGET_TYPE STREQUAL "STATIC_LIBRARY" OR TARGET_TYPE STREQUAL "SHARED_LIBRARY")
        target_link_libraries(utils INTERFACE ${TARGET})
    endif()
endforeach()

# 确保所有库都生成位置无关代码
set_target_properties(utils PROPERTIES
    POSITION_INDEPENDENT_CODE ON
)

# 导出utils库及其所有子库
install(TARGETS utils
    EXPORT utils-targets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)
